import React from 'react';
import {
  AsyncStorage,
} from 'react-native';

import Config from '../config';
import Constants from '../common/Constants';
import BaseModelNew from './BaseModelNew';

export default class PostModel extends BaseModelNew {
  constructor() {
    super();

    this.requestList = this.requestList.bind(this);
    this.requestContent = this.requestContent.bind(this);
    this.requestLike = this.requestLike.bind(this);
    this.requestShoucang = this.requestShoucang.bind(this);
    this.requestMyPostCommentsNum = this.requestMyPostCommentsNum.bind(this);
    this.requestNewsFollowComments = this.requestNewsFollowComments.bind(this);
    this.requestMyPostComments = this.requestMyPostComments.bind(this);
    this.requestMyPushPostsNum = this.requestMyPushPostsNum.bind(this);
    this.requestMyPushPosts = this.requestMyPushPosts.bind(this);
  }

  //请求帖子列表
  requestList(data, callback) {
    //board_id版块ID
    //page 页码
    //page_size  每页数量
    //show_last_days 显示最近几天内的
    //sort_id 排序方式，1最新，2最热, 3点赞, 4评论数
    //tag 可有可无，取决于board_id，具体需与后台确认
    var url = Config.serverUrl + "/boards/" + data.board_id + "/posts?page="+ data.page +
      "&page_size=" + data.page_size + "&sort_id=" + data.sort_id;
    if(data.show_last_days) {
      url = url + "&show_last_days=" + data.show_last_days;
    }
    if(data.tag) {
      url = url + "&tag=" + data.tag;
    }
    this.sendGet(url, callback);
  }

  //请求帖子资料
  requestContent(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id + "/posts/" + data.post_id;
    this.sendGet(url, callback);
  }

  //请求点赞
  requestLike(data, callback) {
    if (data.news_id!=undefined) {
      this.sendPutNew(Config.serverUrl + '/news/' + data.news_id + "/like",
        callback);
    }else{
      this.sendPutNew(Config.serverUrl + '/boards/' + (data.board_id!==undefined?data.board_id:1) + '/posts/' + data.post_id + "/like",
        callback);
    }
  }

  requestShoucang(data, callback) {
    if (data.news_id!=undefined) {
      this.sendPutNew(Config.serverUrl + '/news/' + data.news_id + "/favorite",
      callback);
    }else{
      this.sendPutNew(Config.serverUrl + '/boards/' + (data.board_id!==undefined?data.board_id:1) + '/posts/' + data.post_id + "/favorite",
      callback);
    }    
  }

  //获取我的帖子未读评论数
  //http://118.89.111.124/dqapp/boards/posts/myposts/comments/num?board_type=1
  requestMyPostCommentsNum(data, callback) {
    var url = Config.serverUrl + "/boards/posts/myposts/comments/num?board_type=" + data.board_type;
    this.sendGet(url, callback);
  }

  //获取我的帖子评论列表
  //http://118.89.111.124/dqapp/boards/posts/myposts/comments?board_type=1&page=1&page_size=5
  requestMyPostComments(data, callback) {
    var url = Config.serverUrl + "/boards/posts/myposts/comments?board_type=" + data.board_type + "&page=" + data.page + "&page_size=10";
    this.sendGet(url, callback);
  }

  //获取我的帖子评论追加列表  新闻金点子
  //http://118.89.111.124/dqapp/boards/posts/myposts/comments?board_type=1&page=1&page_size=5
  requestNewsFollowComments(data, callback) {
    var url = Config.serverUrl + "/news/"+ data.news_id+"/comments/" + data.comment_id+"/follows?page=" + data.page;
    this.sendGet(url, callback);
  }

  //获取推送我的帖子列表
  //http://118.89.111.124/dqapp/boards/1/posts/myposts?page=1&page_size=5&tag=1
  requestMyPushPosts(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id + "/posts/myposts?page_size=10&page=" + data.page;
    if(data.tag != undefined) {
      url += "tag=" + data.tag;
    }
    this.sendGet(url, callback);
  }

  //获取推送我的帖子数
  //http://118.89.111.124/dqapp/boards/1/posts/myposts/num?tag=1
  requestMyPushPostsNum(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id + "/posts/myposts/num?";
    if(data.tag != undefined) {
      url += "tag=" + data.tag;
    }
    this.sendGet(url, callback);
  }
}
